<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $editArray = array();
	$editArray['partyId']    = isset($_REQUEST['partyId']) ?  $_REQUEST['partyId'] : 0 ;
	$editArray['partyName']  = "";
	$editArray['mobile1']    = "";
	$editArray['mobile2']    = "";
	$editArray['add1']       = "";
	$editArray['add2']       = "";
	$editArray['taluko']     = "";
	$editArray['dist']       = "";
	$editArray['city']       = "";
	$editArray['pin']        = "";
  	
  if(isset($_POST['partyName']))
  {
    $mobile1      = isset($_POST['mobile1']) ? $_POST['mobile1'] : "";
    $mobile2      = isset($_POST['mobile2'])? $_POST['mobile2'] : "";
    $add1         = isset($_POST['add1']) ? $_POST['add1'] : "";
    $add2         = isset($_POST['add2']) ? $_POST['add2'] : "";
    $taluko       = isset($_POST['taluko)']) ? $_POST['taluko)'] : "";
    $dist         = isset($_POST['dist)']) ? $_POST['dist)'] : "";
    $pin          = isset($_POST['pin)']) ? $_POST['pin)'] : "";
    
    if(isset($_POST['partyId']) && $_POST['partyId'] > 0)
    {       
      $updateTrans = "UPDATE party
                              SET  partyName   = '".$_POST['partyName']."',
                                 	 mobile1  	 = '".$_POST['mobile1']."',
                             	     mobile2  	 = '".$_POST['mobile2']."',
                             	     add1  	     = '".$_POST['add1']."',
                             	     add2  		   = '".$_POST['add2']."',
                             	     taluko  		 = '".$_POST['taluko']."',
                             	     dist  		   = '".$_POST['dist']."',
                             	     cityId  		 = '".$_POST['cityId']."',
                             	     pin  		   = '".$_POST['pin']."'
                             WHERE partyId     = ".$_REQUEST['partyId'];
      $updateTransResult = mysql_query($updateTrans);
      if(!$updateTransResult)
      {
        die("Update Query Not Inserted : ".mysql_error());
      }
      else
      {
        header("Location:party.php");  
        exit();
      }
    }
    else
    {
     $insertParry="INSERT INTO party (partyName,mobile1,mobile2,add1,add2,taluko,dist,cityId,pin) 
      				         VALUES	('".$_POST['partyName']."','".$mobile1."','".$mobile2."','".$add1."','".$add2."','".$_POST['taluko']."',
      				         '".$_POST['dist']."',".$_POST['cityId'].",'".$_POST['pin']."')";
     $insertParryRes = mysql_query($insertParry);
     if(!$insertParryRes)
     {
       die("Insert Query Not Inserted : ".mysql_error());
     }
     else
     {
      header("Location:party.php");  
      exit();
     }
    }  
  }
  
  if (isset ($_GET['partyId']) && $_GET['partyId'] > 0)
  {
    $selectEditParty = "SELECT partyId,partyName,mobile1,mobile2,add1,add2,taluko,dist,party.cityId,pin
                             FROM party
                            LEFT JOIN city ON city.cityId = party.cityId
                           WHERE partyId = ".$_GET['partyId'];
    $selectEditPartyRes = mysql_query($selectEditParty);
    while($editRow = mysql_fetch_array($selectEditPartyRes))
    {
    	$editArray['partyId']   = $editRow['partyId'];
    	$editArray['partyName'] = $editRow['partyName'];
    	$editArray['mobile1']   = $editRow['mobile1'];
    	$editArray['mobile2']   = $editRow['mobile2'];
    	$editArray['add1']      = $editRow['add1'];
    	$editArray['add2']      = $editRow['add2'];
    	$editArray['taluko']    = $editRow['taluko'];
    	$editArray['dist']      = $editRow['dist'];
    	$editArray['cityId']      = $editRow['cityId'];
    	$editArray['pin']       = $editRow['pin'];
    }
  }
  
  $cityArray = array();
  $m = 0;
  $selectCity  = "SELECT cityId,city
                     FROM city";
  $selectCityRes = mysql_query($selectCity);
  while($cityRow = mysql_fetch_array($selectCityRes))
  {
  	$cityArray['cityId'][$m] = $cityRow['cityId'];
  	$cityArray['city'][$m]   = $cityRow['city'];
  	$m++;
  }
}
include("./bottom.php");
$smarty->assign('editArray',$editArray);
$smarty->assign('cityArray',$cityArray);
$smarty->display('party.tpl');
?>
